package com.wolan;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.*;
import org.bson.Document;

import java.util.Iterator;

/**
 * Created by fei.chen on 2016/3/31.
 */
public class MongoTest {
    public static void main(String[] args) {
        MongoClient client = new MongoClient("172.160.100.192",27017);
        MongoIterable<String> dbs =  client.listDatabaseNames();
        for(String db:dbs){
            System.out.println("db name = "+ db);
        }

        MongoDatabase db = client.getDatabase("runoob");
        MongoIterable<String> cols = db.listCollectionNames();
        for(String colName:cols){
            System.out.println("collention name = "+colName);
        }

        MongoCollection col = db.getCollection("cols");
        System.out.println(col.count());

        FindIterable findIterable = col.find(new BasicDBObject("title","java")).sort(new BasicDBObject("_id",-1));
        MongoCursor<Document> cursor = findIterable.iterator();
        while(cursor.hasNext()){
            Document dbo = cursor.next();
            System.out.println(dbo);
            System.out.println(dbo.get("title"));
        }

        MongoCollection colRunoob = db.getCollection("runoob");
        System.out.println(colRunoob);
        //删除文档集合
        colRunoob.drop();



    }
}
